DMelt:Numeric/2 Random Numbers
Random numbers
Read Random_numbers article. The DMelt random numbers can be constructed using several approaches:
- The native Java API for random numbers. For example, see the Java java.util.Random.
- The native Python API for random numbers
- The native DataMelt classes
The Native Java approach
Random numbers provided by Java API have already been used in the can be used to generate a single random number. Below we check the methods of this class:
from java.util import * r=Random() # seed from the system time. r=Random(100L) # user defined seed=100L dir(r)
You will see the methods of the Random class:
[.. 'nextDouble', 'nextFloat', 'nextGaussian', ...'nextInt', 'nextLong' ..]
Here is an example of how to fill a list with Gaussian random numbers:
from java.util import * r=Random() g=[] for i in range(100): g.append(r.nextGaussian())
The Native Python approach
Let us give a simple example which shows how to generate a single random floating point number in the range [0,1] using the Python API:
from random import * r=Random() a=r.randint(1,10) # a random number in range [0.10]
A random seed from the current system time is used since we do not specify any argument for the Random(). In order to generate a random number predictably for debugging purpose, one should pass an integer (or long) value to the Random(), i.e., for example Random(1L).
One can use the following random number methods:
* r.random() # in range [0.0, 1.0) * r.randint(min,max) # int in range [min,max] * r.uniform(min,max) # real number in [min,max] * r.betavariate(a,b) # Beta distribution (a>0,b>0) * r.expovariate(lambda) # Exponential distribution * r.gauss(m,s) # Gaussian distribution with the mean "m" and sigma "s" * r.lognormvariate(m,s) # Log normal distribution with the mean "m" and sigma "s" * r.normalvariate(m,s) # Normal distribution with the mean "m" and sigma "s" * r.gammavariate(a, b) # Gamma distribution. * r.seed(i) # set seed (i integer or long) * state=r.getstate() # returns internal state * setstate(state) # restores internal st
Random numbers are also used for manipulations with lists. One can randomly rearrange elements in a list as:
list=[1,2,3,4,5,6,7,8,9] r.shuffle(list) print list
The code generates this:
[3, 4, 2, 7, 6, 5, 9, 8, 1]
The Native DataMelt approach
Use the package "cern.jet.random" to build random numbers in DataMelt. Check this out as:
import cern.jet.random dir(cern.jet.random)
This will printout the available classes for generation of random distributions:
Beta, Binomial, BreitWigner, BreitWignerMeanSquare, ChiSquare, Empirical, EmpiricalWalker, Exponential, ExponentialPower, Gamma, Hyperbolic, HyperGeometric, Logarithmic, NegativeBinomial, Normal, Poisson, PoissonSlow, StudentT, Uniform, VonMises, Zeta
Let us give an example how to generate 100 integer values distributed in accordance with a Poissonian distribution (with the mean 10)
from cern.jet.random.engine import * from cern.jet.random import * engine=MersenneTwister() poisson=Poisson(10,engine) for i in range(100): print poisson.nextInt()
The MersenneTwister is one of the strongest engines. One can use the current system date for a seed to avoid reproducible random numbers:
from cern.jet.random.engine import * import java engine=MersenneTwister(new java.util.Date())
Distributions from density functions
Here is a code example showing how to generate random distribution from any given function. Read more deatils in jhplot.math.StatisticSample.
from jhplot import * from jhplot.math.StatisticSample import * from java.awt import * c1 = HPlot('Canvas',600,400) c1.setGTitle('Title') c1.visible() c1.setRange(-1,11,0,30) f=F1D('10+2*cos(x)',0,10) f.setColor(Color.red) c1.draw(f) p=f.getParse() a=randomRejection(1000,p,15,0,10) h=H1D('Random numbers',100,0,10) h.fill(a) c1.draw(h)
Third-party libraries
DataMelt contains a number of third-party Java libraries that can be used for computation of density (PDF), cumulative (CDF), quantile, and random variates of many popular statistical distributions, such as:
- Ansari-Bradley
- Beta
- Binomial
- Cauchy
- Chi square
- Exponential
- Fisher's F
- Gamma
- Geometric
- Hypergeometric
- Kendall
- Logistic
- Log normal
- Negative binomial
- Non-central beta,
- Non-central chi square,
- Non-central F
- Non-central T
- Normal
- Poisson
- Sign rank
- Spearman
- Student's T
- Tukey
- Uniform
- Weibull
- Wilcoxon
and many others. Here is the list you can access and include into your programs. Look at the following Java packages: